package com.cw.jeeyt.service.impl.api;

import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.api.ApiFacadeGenerateConfigBo;
import com.cw.jeeyt.service.converter.api.ApiFacadeGenerateConfigConverter;
import com.cw.jeeyt.dao.intf.api.ApiFacadeGenerateConfigDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeGenerateConfigDo;
import com.cw.jeeyt.dao.query.api.ApiFacadeGenerateConfigQuery;
import com.cw.jeeyt.api.dto.facadeExcel.ApiGenerateFacadeExcelDto;
import com.cw.jeeyt.service.intf.api.ApiFacadeGenerateConfigService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口文档生成配置接口业务实现类
 *
 * @author admin
 * @date 2020-04-19 17:18:55
 */
@Service
public class ApiFacadeGenerateConfigServiceImpl implements ApiFacadeGenerateConfigService {
    @Autowired
    private ApiFacadeGenerateConfigDao apiFacadeGenerateConfigDao;

    /**
     * 新增接口文档生成配置
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(ApiFacadeGenerateConfigBo request) {
        if (request == null) {
            return null;
        }
        Long id = SequenceTool.nextLongId();
        request.setId(id);
        request.setCreateTime(new Date());
        request.setUpdateTime(new Date());
        apiFacadeGenerateConfigDao.insertBySelective(ApiFacadeGenerateConfigConverter.bo2Do(request));
        return id;
    }

    /**
     * 更新接口文档生成配置
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(ApiFacadeGenerateConfigBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateTime(new Date());
        apiFacadeGenerateConfigDao.updateBySelective(ApiFacadeGenerateConfigConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口文档生成配置
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(ApiFacadeGenerateConfigBo request) {
        if (request == null) {
            return false;
        }
        return apiFacadeGenerateConfigDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口文档生成配置
     *
     * @param request 条件
     * @return PageInfo<ApiFacadeGenerateConfigBo>
     */
    @Override
    public PageInfo<ApiFacadeGenerateConfigBo> selectPage(ApiFacadeGenerateConfigBo request, PageParam pageParam) {
        PageInfo<ApiFacadeGenerateConfigDo> pageInfo = apiFacadeGenerateConfigDao.selectPage(
            ApiFacadeGenerateConfigQuery.builder()
                .enumSacnPackage(request.getEnumSacnPackage())
                .facadeParamFilePaths(request.getFacadeParamFilePaths())
                .facadeUrlPrefix(request.getFacadeUrlPrefix())
                .generateHeaderParam(request.getGenerateHeaderParam())
                .id(request.getId())
                .outputPath(request.getOutputPath())
                .projectCode(request.getProjectCode())
                .projectName(request.getProjectName())
                .targetPath(request.getTargetPath())
                .build(), pageParam);
        List<ApiFacadeGenerateConfigBo> apiFacadeGenerateConfigs = ApiFacadeGenerateConfigConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(apiFacadeGenerateConfigs)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("generateHeaderParam", "std_zb_yn");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(apiFacadeGenerateConfigs)),
                dictMap);
            apiFacadeGenerateConfigs = JSON.parseArray(jsonArray.toJSONString(), ApiFacadeGenerateConfigBo.class);
        }
        return new PageInfo<>(pageInfo, apiFacadeGenerateConfigs);
    }

    /**
     * 查询接口文档生成配置详情信息
     *
     * @param request 条件
     * @return ApiFacadeGenerateConfigBo
     */
    @Override
    public ApiFacadeGenerateConfigBo queryDetail(ApiFacadeGenerateConfigBo request) {
        if (request == null) {
            return null;
        }
        return ApiFacadeGenerateConfigConverter.do2Bo(apiFacadeGenerateConfigDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口文档生成配置
     *
     * @param request 条件
     * @return List<ApiFacadeGenerateConfigBo>
     */
    @Override
    public List<ApiFacadeGenerateConfigBo> queryList(ApiFacadeGenerateConfigBo request) {
        if (request == null) {
            return null;
        }
        return ApiFacadeGenerateConfigConverter.doList2BoList(apiFacadeGenerateConfigDao.selectByExample(
            ApiFacadeGenerateConfigQuery.builder()
                .enumSacnPackage(request.getEnumSacnPackage())
                .facadeParamFilePaths(request.getFacadeParamFilePaths())
                .facadeUrlPrefix(request.getFacadeUrlPrefix())
                .generateHeaderParam(request.getGenerateHeaderParam())
                .id(request.getId())
                .outputPath(request.getOutputPath())
                .projectCode(request.getProjectCode())
                .projectName(request.getProjectName())
                .targetPath(request.getTargetPath())
                .build()));
    }

    /**
     * 根据key查询接口文档生成配置
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeGenerateConfigBo selectByKey(Long id) {

        return ApiFacadeGenerateConfigConverter.do2Bo(apiFacadeGenerateConfigDao.selectByKey(id));
    }

    @Override
    public void saveGenerateConfig(ApiGenerateFacadeExcelDto facadeExcelDto) {
        ApiFacadeGenerateConfigDo generateConfigDo = ApiFacadeGenerateConfigConverter.facadeExcel2Do(facadeExcelDto);
        generateConfigDo.setUpdateTime(new Date());
        List<ApiFacadeGenerateConfigDo> configDos = apiFacadeGenerateConfigDao.selectByExample(
            ApiFacadeGenerateConfigQuery.builder().build());
        if(CollectionUtil.isNotEmpty(configDos)){
            generateConfigDo.setId(configDos.get(0).getId());
            apiFacadeGenerateConfigDao.updateBySelective(generateConfigDo);
        }else{
            generateConfigDo.setCreateTime(new Date());
            generateConfigDo.setId(SequenceTool.nextLongId());
            apiFacadeGenerateConfigDao.insertBySelective(generateConfigDo);
        }
    }
}
